|
1
|
|
|
/* |
|
2
|
|
|
* Copyright (c) 2017-2018 Rafael da Silva Rocha. |
|
3
|
|
|
* |
|
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
|
5
|
|
|
* a copy of this software and associated documentation files (the |
|
6
|
|
|
* "Software"), to deal in the Software without restriction, including |
|
7
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
|
8
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
|
9
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
|
10
|
|
|
* the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be |
|
13
|
|
|
* included in all copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
16
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
17
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
18
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|
19
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
20
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|
21
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @fileoverview The byte-data API. |
|
27
|
|
|
* @see https://github.com/rochars/byte-data |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
/** @module byteData */ |
|
31
|
|
|
import endianness from './lib/endianness.js'; |
|
32
|
|
|
import {reader_, setUp_, writeBytes_, |
|
33
|
|
|
fromBytes_, toBytes_} from './lib/packer.js'; |
|
34
|
|
|
import {validateType, validateNotUndefined} from './lib/validation.js'; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
// Strings |
|
37
|
|
|
/** |
|
38
|
|
|
* Read a string from a byte buffer. |
|
39
|
|
|
* @param {!Uint8Array} bytes A byte buffer. |
|
40
|
|
|
* @param {number=} index The index to read. |
|
41
|
|
|
* @param {?number=} len The number of bytes to read. |
|
42
|
|
|
* @return {string} |
|
43
|
|
|
*/ |
|
44
|
|
|
export function unpackString(bytes, index=0, len=null) { |
|
45
|
|
|
let chrs = ''; |
|
46
|
|
|
len = len || bytes.length - index; |
|
47
|
|
|
for(let j = 0; j < len; j++) { |
|
48
|
|
|
chrs += String.fromCharCode(bytes[index+j]); |
|
49
|
|
|
} |
|
50
|
|
|
return chrs; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Write a string as a byte buffer. |
|
55
|
|
|
* @param {string} str The string to pack. |
|
56
|
|
|
* @return {!Array<number>} The next index to write on the buffer. |
|
57
|
|
|
*/ |
|
58
|
|
|
export function packString(str) { |
|
59
|
|
|
let bytes = []; |
|
60
|
|
|
for (let i = 0; i < str.length; i++) { |
|
61
|
|
|
bytes[i] = str.charCodeAt(i); |
|
62
|
|
|
} |
|
63
|
|
|
return bytes; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Write a string to a byte buffer. |
|
68
|
|
|
* @param {string} str The string to pack. |
|
69
|
|
|
* @param {!Uint8Array} bytes A byte buffer. |
|
70
|
|
|
* @param {number=} index The index to write in the buffer. |
|
71
|
|
|
* @return {number} The next index to write in the buffer. |
|
72
|
|
|
*/ |
|
73
|
|
|
export function packStringTo(str, bytes, index=0) { |
|
74
|
|
|
for (let i = 0; i < str.length; i++) { |
|
75
|
|
|
bytes[index] = str.charCodeAt(i); |
|
76
|
|
|
index++; |
|
77
|
|
|
} |
|
78
|
|
|
return index; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// Numbers |
|
82
|
|
|
/** |
|
83
|
|
|
* Pack a number as a byte buffer. |
|
84
|
|
|
* @param {number} value The number. |
|
85
|
|
|
* @param {!Object} theType The type definition. |
|
86
|
|
|
* @return {!Array<number>} The packed value. |
|
87
|
|
|
* @throws {Error} If the type definition is not valid. |
|
88
|
|
|
* @throws {Error} If the value is not valid. |
|
89
|
|
|
*/ |
|
90
|
|
|
export function pack(value, theType) { |
|
91
|
|
|
setUp_(theType); |
|
92
|
|
|
return toBytes_([value], theType); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Pack an array of numbers as a byte buffer. |
|
97
|
|
|
* @param {!Array<number>} values The values. |
|
98
|
|
|
* @param {!Object} theType The type definition. |
|
99
|
|
|
* @return {!Array<number>} The packed values. |
|
100
|
|
|
* @throws {Error} If the type definition is not valid. |
|
101
|
|
|
* @throws {Error} If any of the values are not valid. |
|
102
|
|
|
*/ |
|
103
|
|
|
export function packArray(values, theType) { |
|
104
|
|
|
setUp_(theType); |
|
105
|
|
|
return toBytes_(values, theType); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Pack a number to a byte buffer. |
|
110
|
|
|
* @param {number} value The value. |
|
111
|
|
|
* @param {!Object} theType The type definition. |
|
112
|
|
|
* @param {!Uint8Array} buffer The output buffer. |
|
113
|
|
|
* @param {number=} index The index to write. |
|
114
|
|
|
* @return {number} The next index to write. |
|
115
|
|
|
* @throws {Error} If the type definition is not valid. |
|
116
|
|
|
* @throws {Error} If the value is not valid. |
|
117
|
|
|
*/ |
|
118
|
|
|
export function packTo(value, theType, buffer, index=0) { |
|
119
|
|
|
setUp_(theType); |
|
120
|
|
|
return writeBytes_(value, |
|
121
|
|
|
theType, |
|
122
|
|
|
buffer, |
|
123
|
|
|
index, |
|
124
|
|
|
index + theType.offset, |
|
125
|
|
|
validateNotUndefined, |
|
126
|
|
|
theType.be); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Pack a array of numbers to a byte buffer. |
|
131
|
|
|
* @param {!Array<number>} values The value. |
|
132
|
|
|
* @param {!Object} theType The type definition. |
|
133
|
|
|
* @param {!Uint8Array} buffer The output buffer. |
|
134
|
|
|
* @param {number=} index The buffer index to write. |
|
135
|
|
|
* @return {number} The next index to write. |
|
136
|
|
|
* @throws {Error} If the type definition is not valid. |
|
137
|
|
|
* @throws {Error} If the value is not valid. |
|
138
|
|
|
*/ |
|
139
|
|
|
export function packArrayTo(values, theType, buffer, index=0) { |
|
140
|
|
|
setUp_(theType); |
|
141
|
|
|
let be = theType.be; |
|
142
|
|
|
let offset = theType.offset; |
|
143
|
|
|
for (let i=0; i<values.length; i++) { |
|
144
|
|
|
index = writeBytes_( |
|
145
|
|
|
values[i], |
|
146
|
|
|
theType, |
|
147
|
|
|
buffer, |
|
148
|
|
|
index, |
|
149
|
|
|
index + offset, |
|
150
|
|
|
validateNotUndefined, |
|
151
|
|
|
be); |
|
152
|
|
|
} |
|
153
|
|
|
return index; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Unpack a number from a byte buffer. |
|
158
|
|
|
* @param {!Uint8Array} buffer The byte buffer. |
|
159
|
|
|
* @param {!Object} theType The type definition. |
|
160
|
|
|
* @return {number} |
|
161
|
|
|
* @throws {Error} If the type definition is not valid |
|
162
|
|
|
*/ |
|
163
|
|
|
export function unpack(buffer, theType) { |
|
164
|
|
|
setUp_(theType); |
|
165
|
|
|
let values = fromBytes_( |
|
166
|
|
|
buffer.slice(0, theType.offset), theType); |
|
167
|
|
|
return values[0]; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Unpack an array of numbers from a byte buffer. |
|
172
|
|
|
* @param {!Uint8Array} buffer The byte buffer. |
|
173
|
|
|
* @param {!Object} theType The type definition. |
|
174
|
|
|
* @return {!Array<number>} |
|
175
|
|
|
* @throws {Error} If the type definition is not valid. |
|
176
|
|
|
*/ |
|
177
|
|
|
export function unpackArray(buffer, theType) { |
|
178
|
|
|
setUp_(theType); |
|
179
|
|
|
return fromBytes_(buffer, theType); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Unpack a number from a byte buffer by index. |
|
184
|
|
|
* @param {!Uint8Array} buffer The byte buffer. |
|
185
|
|
|
* @param {!Object} theType The type definition. |
|
186
|
|
|
* @param {number=} index The buffer index to read. |
|
187
|
|
|
* @return {number} |
|
188
|
|
|
* @throws {Error} If the type definition is not valid |
|
189
|
|
|
*/ |
|
190
|
|
|
export function unpackFrom(buffer, theType, index=0) { |
|
191
|
|
|
setUp_(theType); |
|
192
|
|
|
if (theType.be) { |
|
193
|
|
|
endianness(buffer, theType.offset, index, index + theType.offset); |
|
194
|
|
|
} |
|
195
|
|
|
let value = reader_(buffer, index); |
|
196
|
|
|
if (theType.be) { |
|
197
|
|
|
endianness(buffer, theType.offset, index, index + theType.offset); |
|
198
|
|
|
} |
|
199
|
|
|
return value; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Unpack a array of numbers from a byte buffer by index. |
|
204
|
|
|
* @param {!Uint8Array} buffer The byte buffer. |
|
205
|
|
|
* @param {!Object} theType The type definition. |
|
206
|
|
|
* @param {number=} start The start index. Assumes 0. |
|
207
|
|
|
* @param {?number=} end The end index. Assumes the buffer length. |
|
208
|
|
|
* @return {!Array<number>} |
|
209
|
|
|
* @throws {Error} If the type definition is not valid |
|
210
|
|
|
*/ |
|
211
|
|
|
export function unpackArrayFrom(buffer, theType, start=0, end=null) { |
|
212
|
|
|
setUp_(theType); |
|
213
|
|
|
if (theType.be) { |
|
214
|
|
|
endianness(buffer, theType.offset); |
|
215
|
|
|
} |
|
216
|
|
|
let len = end || buffer.length; |
|
217
|
|
|
let values = []; |
|
218
|
|
|
for (let i=start; i<len; i+=theType.offset) { |
|
219
|
|
|
values.push(reader_(buffer, i)); |
|
220
|
|
|
} |
|
221
|
|
|
if (theType.be) { |
|
222
|
|
|
endianness(buffer, theType.offset); |
|
223
|
|
|
} |
|
224
|
|
|
return values; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Unpack a array of numbers to a typed array. |
|
229
|
|
|
* @param {!Uint8Array} buffer The byte buffer. |
|
230
|
|
|
* @param {!Object} theType The type definition. |
|
231
|
|
|
* @param {!TypedArray} output The output array. |
|
232
|
|
|
* @throws {Error} If the type definition is not valid |
|
233
|
|
|
*/ |
|
234
|
|
|
export function unpackArrayTo(buffer, theType, output) { |
|
235
|
|
|
setUp_(theType); |
|
236
|
|
|
if (theType.be) { |
|
237
|
|
|
endianness(buffer, theType.offset); |
|
238
|
|
|
} |
|
239
|
|
|
let len = buffer.length; |
|
240
|
|
|
let outputIndex = 0; |
|
241
|
|
|
for (let i=0; i<len; i+=theType.offset) { |
|
242
|
|
|
output.set([reader_(buffer, i)], outputIndex); |
|
243
|
|
|
outputIndex++; |
|
244
|
|
|
} |
|
245
|
|
|
if (theType.be) { |
|
246
|
|
|
endianness(buffer, theType.offset); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|